<template>
	<view class="l-nav-bar" :style="{marginTop: `${statusHeight}px`}">
		<view class="l-nav-bar-left">
			<view class="l-nav-bar-left-content" v-if="back">
				<slot name="left">
					<i class="l-left-icon l-icon-back" @click="goBack"></i>
					<text class="l-left-label" @click="goBack">返回</text>
				</slot>
			</view>
		</view>
		<view class="l-nav-bar-center">
			<slot name="center">{{title}}</slot>
		</view>
		<view class="l-nav-bar-right">
			<slot name="right"></slot>
		</view>
	</view>
</template>

<script>
	export default {
		name:"l-nav-bar",
		props: {
			// 是否能够返回
			back: {
				type: Boolean,
				default: false
			},
			// 自定义导航栏标题
			title: {
				type: String,
				default: "标题"
			}
		},
		data() {
			return {
				statusHeight: 0,
			};
		},
		methods: {
			// 返回上一页
			goBack(){
				uni.navigateBack({
					animationDuration: 300,
					animationType: "fade-out"
				})
			}
		},
		mounted() {
			const that = this;
			// 设备状态栏高度，防止自定义导航栏显示异常
			uni.getSystemInfo({
				success(res){
					that.statusHeight = res.safeArea.top;
				}
			})
		}
	}
</script>

<style>
@import url(./iconfont.css);
.l-nav-bar{
	display: flex;
	width: 100vw;
	height: 40px;
	line-height: 40px;
	box-shadow: 0px 1px 1px 0px rgba(0, 0, 0, .2);
}
.l-nav-bar>view{
	flex: 1;
	font-size: 18px;
}
.l-nav-bar-left{
	font-size: 18px;
	color: #1f90d6;
	text-align: left;
	padding-left: 8px;
}
.l-nav-bar-left i{
	padding: 0 5px 0 0;
}
.l-nav-bar-center{
	text-align: center;
}
.l-nav-bar-right{
	text-align: right;
	padding-right: 8px;
}
</style>